﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Data;
using CUREEntity.Harmful;
using System.Web.Security;
using DBUtility;
using System.Web;
using System.Collections;
using CUREDataAccessLayers.Common;
/*************************************************************************************

     liudan  add   技术判定书
  
*************************************************************************************/

namespace CUREDataAccessLayers.Harmful
{
    public class TBookDal
    {
        private common comm = new common();

        /// <summary>
        /// 获取待登记不良胶列表
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public DataSet GetLedgerList(IDictionary<string, object> where, int pageIndex, int pageSize, out int totalRecordCount, out int totalPageCount)
        {
            DataSet ds = new DataSet();
            StringBuilder commandText = new StringBuilder();
            totalRecordCount = 0;
            totalPageCount = 0;
            string fid = string.Empty;

            if (where.ContainsKey("fid") && where["fid"] != null && where["fid"].ToString() != "")
            {
                fid = where["fid"].ToString();
            }

            commandText.Append(" select ID,FID,DeviceNo,Spec,ProMonth,ProDay,OrderBatch,TrayNo,TrayBatch, ");
            commandText.Append(" ExpireMonth,ExpireDay,ProTime,Weight,Reason,FClass,");
            commandText.Append(" Machine,Material,FilmModel,OilModel,ActualNum,BTNo,BJBTNO,OkMan,Field, ");
            commandText.Append(" MakeMan,Skiller,to_char(HandlerTime,'yyyy-mm-dd HH24:mi:ss')HandlerTime, ");
            commandText.Append(" FClass2,Machine2,SubModel,SubBTNum,Interfuse,Residual,Remarks,State,Updater ");
            commandText.Append(" from Harmful_Ledger  where 1=1 and State=" + where["state"].ToString() + " ");

            if (fid != null && fid != "")
            {
                commandText.Append(" and FID like '%" + fid.Trim() + "%'");
            }

            try
            {
                ds = PageHelper.GetPagedData(pageIndex, pageSize, commandText.ToString(), "  ID asc ", out totalRecordCount, out totalPageCount);
            }
            catch (Exception ex)
            {
                //comm.InsertLog(ex.Message);
            }
            return ds;
        }
        /// <summary>
        /// 获取技术判定书是新增还是编辑
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public string GetAction(string hid, out string id)
        {
            string ls_return = "addTBook";
            id = "";

            DataSet dt = new DataSet();
            StringBuilder commandText = new StringBuilder();
            commandText.Append(" select ID from Technology_Book ");
            commandText.Append(" where HID= '" + hid + "'");
            try
            {
                dt = DbHelperOra.Query(commandText.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            if (dt != null)
            {
                foreach (DataRow dr in dt.Tables[0].Rows)
                {
                    ls_return = "editTBook";
                    id = dr[0].ToString().Trim();
                }
            }
            else
            {
                ls_return = "addTBook";
            }

            return ls_return;
        }
        /// <summary>
        /// 获取技术判定书是新增还是编辑
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public DataTable GetBook(string id) 
        {
            DataTable dt = new DataTable();

            StringBuilder commandText = new StringBuilder();
            commandText.Append("select ID,HID,FID,Saccepter,to_char(AcceptDate,'yyyy-mm-dd') AcceptDate,Stage,Model,Category, ");
            commandText.Append("to_char(Fdate,'yyyy-mm-dd') Fdate,Amount,Jaccepter,to_char(Sdate,'yyyy-mm-dd') Sdate,Method, ");
            commandText.Append("to_char(MDate,'yyyy-mm-dd') MDate,to_char(PDDate,'yyyy-mm-dd') PDDate,to_char(JCDate,'yyyy-mm-dd') JCDate, ");
            commandText.Append("to_char(FHDate,'yyyy-mm-dd') FHDate,to_char(PDSDate,'yyyy-mm-dd') PDSDate,to_char(BackDate,'yyyy-mm-dd') BackDate,");
            commandText.Append("Result,Remarks,Recorder,to_char(RecordDate,'yyyy-mm-dd hh24:mi:ss') RecordDate ");
            commandText.Append("from Technology_Book ");
            commandText.Append("where ID ='" + id + "'");

            try
            {
                dt = DbHelperOra.QueryTable(commandText.ToString());
            }
            catch (Exception ex)
            {
                comm.InsertLog(ex.Message);
            }
            return dt;
        }
        /// <summary>
        /// 新增获取ID
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public string GetID() 
        {
            string ls_return = "";

            DataTable dt = new DataTable();
            StringBuilder commandText = new StringBuilder();
            commandText.Append(" select SEQ_Technology_Book.nextval id from dual ");
            try
            {
                dt = DbHelperOra.QueryTable(commandText.ToString());
                ls_return = dt.Rows[0]["id"].ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ls_return;
        }
        /// <summary>
        /// 新增技术判定书
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public bool InsertBook(TBookModel tbModel) 
        {
            bool lb_return = false;
            StringBuilder commandText = new StringBuilder();
            string states = "-2";
            if (tbModel.Result.Trim() == "OK") { states = "99"; }

            commandText.Append(" insert into Technology_Book(ID,HID,FID,Saccepter,AcceptDate,Stage,");
            commandText.Append(" Model,Category,Fdate,Amount,Jaccepter,Sdate,Method,MDate,PDDate,");
            commandText.Append(" JCDate,FHDate,PDSDate,BackDate,Result,Remarks,Recorder,RecordDate)");
            commandText.Append(" values(");
            commandText.Append("'" + tbModel.ID + "',"); 
            commandText.Append("'" + tbModel.HID + "',");
            commandText.Append("'" + tbModel.FID + "',");
            commandText.Append("'" + tbModel.Saccepter + "',");
            commandText.Append("to_date('" + tbModel.AcceptDate + "','yyyy-mm_dd'),"); 
            
            commandText.Append("'" + tbModel.Stage + "',");
            commandText.Append("'" + tbModel.Model + "',");
            commandText.Append("'" + tbModel.Category + "',");
            commandText.Append("to_date('" + tbModel.Fdate + "','yyyy-mm_dd'),");
            commandText.Append("'" + tbModel.Amount + "',");

            commandText.Append("'" + tbModel.Jaccepter + "',");
            commandText.Append("to_date('" + tbModel.Sdate + "','yyyy-mm_dd'),");
            commandText.Append("'" + tbModel.Method + "',");
            commandText.Append("to_date('" + tbModel.MDate + "','yyyy-mm_dd'),");
            commandText.Append("to_date('" + tbModel.PDDate + "','yyyy-mm_dd'),");

            commandText.Append("to_date('" + tbModel.JCDate + "','yyyy-mm_dd'),");
            commandText.Append("to_date('" + tbModel.FHDate + "','yyyy-mm_dd'),");
            commandText.Append("to_date('" + tbModel.PDSDate + "','yyyy-mm_dd'),");
            commandText.Append("to_date('" + tbModel.BackDate + "','yyyy-mm_dd'),"); 
            commandText.Append("'" + tbModel.Result + "',");

            commandText.Append("'" + tbModel.Remarks + "',");
            commandText.Append("'" + tbModel.Recorder + "',");
            commandText.Append("to_date('" + tbModel.RecordDate + "','yyyy-mm_dd hh24:mi:ss')");

            commandText.Append(")");

            try
            {
                int res = DbHelperOra.ExecuteSql(commandText.ToString());
                if (res > 0)
                {
                    if (UpdateLedger(tbModel.HID, states, tbModel.Recorder))
                    {
                        lb_return = true;
                    }    
                }
            }
            catch (Exception ex)
            {
                comm.InsertLog(ex.Message);
            }
            return lb_return;

        }
        /// <summary>
        /// 更新技术判定书
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public bool UpdateBook(TBookModel tbModel) 
        {
            bool lb_return = false;
            StringBuilder commandText = new StringBuilder();
            string states = "-2";
            if (tbModel.Result.Trim() == "OK") { states = "99"; }

            commandText.Append(" update Technology_Book set ");
            commandText.Append(" Saccepter ='" + tbModel.Saccepter + "',");
            commandText.Append(" AcceptDate =to_date('" + tbModel.AcceptDate + "','yyyy-mm-dd'),");
            commandText.Append(" Stage ='" + tbModel.Stage + "',");
            commandText.Append(" Model ='" + tbModel.Model + "',");
            commandText.Append(" Category ='" + tbModel.Category + "',");

            commandText.Append(" Fdate =to_date('" + tbModel.Fdate + "','yyyy-mm-dd'),");
            commandText.Append(" Amount ='" + tbModel.Amount + "',");
            commandText.Append(" Jaccepter ='" + tbModel.Jaccepter + "',");
            commandText.Append(" Sdate =to_date('" + tbModel.Sdate + "','yyyy-mm-dd'),");
            commandText.Append(" Method ='" + tbModel.Method + "',");

            commandText.Append(" MDate =to_date('" + tbModel.MDate + "','yyyy-mm-dd'),");
            commandText.Append(" PDDate =to_date('" + tbModel.PDDate + "','yyyy-mm-dd'),");
            commandText.Append(" JCDate =to_date('" + tbModel.JCDate + "','yyyy-mm-dd'),");
            commandText.Append(" FHDate =to_date('" + tbModel.FHDate + "','yyyy-mm-dd'),");
            commandText.Append(" PDSDate =to_date('" + tbModel.PDSDate + "','yyyy-mm-dd'),");

            commandText.Append(" BackDate =to_date('" + tbModel.BackDate + "','yyyy-mm-dd'),");
            commandText.Append(" Result ='" + tbModel.Result + "',");
            commandText.Append(" Remarks ='" + tbModel.Remarks + "',");
            commandText.Append(" Recorder ='" + tbModel.Recorder + "',");
            commandText.Append(" RecordDate =to_date('" + tbModel.RecordDate + "','yyyy-mm-dd hh24:mi:ss') ");

            commandText.Append(" where ID='" + tbModel.ID + "'");

            try
            {
                int res = DbHelperOra.ExecuteSql(commandText.ToString());
                if (res > 0)
                {
                    if (UpdateLedger(tbModel.HID, states, tbModel.Recorder)) 
                    {
                        lb_return = true;
                    }                   
                }
            }
            catch (Exception ex)
            {
                comm.InsertLog(ex.Message);
            }
            return lb_return;
        }
        /// <summary>
        /// 更新技术判定书
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public bool UpdateLedger(string hid, string states, string user)
        {
            bool lb_return = false;
            StringBuilder commandText = new StringBuilder();
            commandText.Append(" update Harmful_Ledger set STATE=" + states);
            commandText.Append(" ,UPDATER='" + user + "'");
            commandText.Append(" where ID='" + hid + "'");
            try
            {
                int res = DbHelperOra.ExecuteSql(commandText.ToString());
                if (res > 0)
                {
                    lb_return = true;
                }
            }
            catch (Exception ex)
            {
                comm.InsertLog(ex.Message);
            }
            return lb_return;
        }

    }
}
